<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="eduCourse.title" placeholder="课程名称" />
      </el-form-item>

      <el-form-item>
        <el-select v-model="eduCourse.status" clearable placeholder="课程状态">
          <el-option value="Normal" label="已发布" />
          <el-option value="Draft" label="未发布" />
        </el-select>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getAllCourse()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table v-loading="listLoading"
        :data="courseList"
        element-loading-text="数据加载中"
        border
        fit
        highlight-current-row
        row-class-name="myClassList">
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">{{ (current - 1) * size + scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column prop="title" label="课程信息" width="470" align="center">
          <template slot-scope="scope">
            <div class="info">
                <div class="pic">
                <img :src="scope.row.cover" alt="scope.row.title" width="150px">
                </div>
                <div class="title">
                <a href="">{{ scope.row.title }}</a>
                <p>{{ scope.row.totalHours }}课时</p>
                </div>
            </div>
         </template>
      </el-table-column>

      <el-table-column label="是否已发布" width="100">
        <template slot-scope="scope">{{ scope.row.status==="Normal"?'已发布':'未发布' }}</template>
      </el-table-column>

      <el-table-column prop="viewCount" label="浏览量" width="70" />

      <el-table-column prop="createTime" label="添加时间" />

      <el-table-column prop="totalHours" label="课时" width="60" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/course/courseInfo/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑基本信息</el-button>
          </router-link>
          <router-link :to="'/course/courseChapter/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑课程大纲</el-button>
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
          >删除课程信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="current"
      :page-size="size"
      :total="total"
      style="padding: 60px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getAllCourse"
    />
  </div>
</template>

<script>
//1.引入js文件
import course from '@/api/course/course'

export default {
    //写核心代码
    // data:{
    // },
    data(){//定义变量和初始值
        return{
            listLoading: true, // 是否显示loading信息
            courseList: [], //查询之后接口返回的结果集合
            current: 1,
            size: 3,
            total: 10,
            eduCourse: {} //放查询条件,由查询搜索框内容双向绑定
        }
    },
    created(){//页面渲染前之前
        //调用方法
        this.getAllCourse()
    },
    methods:{//创建方法，调用js定义的方法
        //课程列表方法
        getAllCourse(current = 1) {
            this.current = current; //方法已经封装，前端点击page页码，这里能够拿到更新的page页码
            course.getCourseListPage(this.current, this.size, this.eduCourse)
                .then(response => {
                this.courseList = response.data.datas;
                this.total = response.data.total;
                this.listLoading = false
                })
                .catch(error => {
                //请求失败
                console.log(error);
                });
        },
        resetData(){//清空
            //输入项清空
            this.eduCourse = {};
            //查询所有
            this.getAllCourse();

        },
        removeDataById(id){//逻辑删除教师
            
            this.$confirm('此操作将删除该课程, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {//确定删除
                course.deleteCourseById(id)
                    .then(response =>{//删除成功
                        //提示删除成功
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        //刷新列表
                        this.getAllCourse();
                    })
                    .catch(error =>{//删除失败
                        this.$message({
                            type: 'error',
                            message: '删除失败!'
                        });
                    })
            
                }).catch(() => {//取消删除
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        }
    }
}
</script>

<style scoped>
.myClassList .info {
    width: 450px;
    overflow: hidden;
}
.myClassList .info .pic {
    width: 150px;
    height: 90px;
    overflow: hidden;
    float: left;
}
.myClassList .info .pic a {
    display: block;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
.myClassList .info .pic img {
    display: block;
    width: 100%;
}
.myClassList td .info .title {
    width: 280px;
    float: right;
    height: 90px;
}
.myClassList td .info .title a {
    display: block;
    height: 48px;
    line-height: 24px;
    overflow: hidden;
    color: #00baf2;
    margin-bottom: 12px;
}
.myClassList td .info .title p {
    line-height: 20px;
    margin-top: 5px;
    color: #818181;
}
</style>